package com.example.muchentuner.metro;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.example.muchentuner.R;

public class MetroView extends View {

    private Canvas canvas;
    private float x;
    private float y;

    public MetroView(Context context) {
        super(context);
    }
    public MetroView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthSize, heightSize);
        x=widthSize/2;
        y=heightSize/2;
    }

    public static float current_angle;
    public static int show_tempo;
    public static int show_beats;
    public static int show_subdiv;
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;

        drawRect();
        drawMetro();
        drawPendl();
        drawNum();
        postInvalidate();//为啥会顺滑这么多哈哈哈
    }

    private void drawNum(){
        Paint rPaint =new Paint();
        rPaint.setColor(getResources().getColor(R.color.metro_rrecf2));
        rPaint.setAntiAlias(true);
        rPaint.setStyle(Paint.Style.FILL);
        RectF rrectf=new RectF(x-360,y+180,x-60,y+300);
        canvas.drawRoundRect(rrectf,45,45,rPaint);
        RectF rrectf2=new RectF(x+25,y+180,x+360,y+300);
        canvas.drawRoundRect(rrectf2,45,45,rPaint);

        Paint textPaint =new Paint();
        textPaint.setColor(getResources().getColor(R.color.theme_blue));
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(70f);
        Paint textPaint2 =new Paint();
        textPaint2.setColor(getResources().getColor(R.color.theme_orange));
        textPaint2.setAntiAlias(true);
        textPaint2.setTextSize(40f);

        canvas.drawText("BPM",x-325,y+255,textPaint2);
        canvas.drawText(String.valueOf(show_tempo),x-220,y+265,textPaint);

        canvas.drawText("节奏",x+60,y+255,textPaint2);
        canvas.drawText(String.valueOf(show_beats),x+170,y+265,textPaint);
        canvas.drawText("/",x+230,y+265,textPaint);
        canvas.drawText(String.valueOf(show_subdiv),x+280,y+265,textPaint);
    }

    private void drawRect(){
        Paint rPaint =new Paint();
        rPaint.setColor(getResources().getColor(R.color.metro_rrecf));
        rPaint.setAntiAlias(true);
        rPaint.setStyle(Paint.Style.FILL);
        RectF rrectf=new RectF(x-450,y-500,x+450,y+400);
        canvas.drawRoundRect(rrectf,45,45,rPaint);
    }

    private void drawPendl(){
        Paint PendlPaint =new Paint();
        PendlPaint.setColor(getResources().getColor(R.color.white));
        PendlPaint.setAntiAlias(true);
        PendlPaint.setStyle(Paint.Style.STROKE);
        PendlPaint.setStrokeWidth(8f);

        float metro_bottom=y+100;
        float triangleH=700f;
        float line1=60;  float line2=185;
        float line3=(float) (1.1*line1);  float line4 =(float)(0.8*line2) ;
        float H3=(1-line3/line2)*triangleH;
        float H4=(1-line4/line2)*triangleH;
        Path Ppath=new Path();
        Ppath.moveTo(x,metro_bottom-H4);
        Ppath.lineTo(x,metro_bottom-H3+10);

        Matrix matrix=new Matrix();
        matrix.postRotate(current_angle,x,metro_bottom-H4);
        Ppath.transform(matrix);
        canvas.drawPath(Ppath,PendlPaint);

        Paint Pendl2Paint =new Paint();
        Pendl2Paint.setColor(getResources().getColor(R.color.metro_pendl));
        Pendl2Paint.setAntiAlias(true);
        Pendl2Paint.setStyle(Paint.Style.FILL);
        Path p2path=new Path();
        p2path.moveTo(x-14,metro_bottom-H3+30);
        p2path.lineTo(x+14,metro_bottom-H3+30);
        p2path.lineTo(x+14,metro_bottom-H3+65);
        p2path.lineTo(x-14,metro_bottom-H3+65);
        p2path.transform(matrix);
        canvas.drawPath(p2path,Pendl2Paint);
    }

    private void drawMetro(){

        Paint metroPaint =new Paint();
        metroPaint.setColor(getResources().getColor(R.color.theme_blue));
        metroPaint.setAntiAlias(true);
        metroPaint.setStyle(Paint.Style.FILL);
        Paint metroPaint2 =new Paint();
        metroPaint2.setColor(getResources().getColor(R.color.metro_shalow_black));
        metroPaint2.setAntiAlias(true);
        metroPaint2.setStyle(Paint.Style.FILL);
        Paint metroPaint3 =new Paint();
        metroPaint3.setColor(getResources().getColor(R.color.metro_baby_grey));
        metroPaint3.setAntiAlias(true);
        metroPaint3.setStyle(Paint.Style.FILL);

        float metro_bottom=y+100;
        float triangleH=700f;
        float line1=60;  float line2=185;
        float line3=(float) (1.1*line1);  float line4 =(float)(0.8*line2) ;
        float H2=(1-line1/line2)*triangleH;
        float H3=(1-line3/line2)*triangleH;
        float H4=(1-line4/line2)*triangleH;
        float metroH=H2+30;

        Path metroPath =new Path();
        metroPath.moveTo(x,metro_bottom-metroH);
        metroPath.lineTo(x+line1,metro_bottom-H2);
        metroPath.lineTo(x+line2,metro_bottom);
        metroPath.lineTo(x-line2,metro_bottom);
        metroPath.lineTo(x-line1,metro_bottom-H2);
        metroPath.close();
        canvas.drawPath(metroPath,metroPaint);

        Path metroPath2 =new Path();
        metroPath2.moveTo(x-line3,metro_bottom-H3);
        metroPath2.lineTo(x+line3,metro_bottom-H3);
        metroPath2.lineTo(x+line4,metro_bottom-H4);
        metroPath2.lineTo(x-line4,metro_bottom-H4);
        metroPath2.close();
        canvas.drawPath(metroPath2,metroPaint2);

        Path metroPath3 =new Path();
        metroPath3.moveTo(x-17,metro_bottom-H3);
        metroPath3.lineTo(x-2,metro_bottom-H3);
        metroPath3.lineTo(x-2,metro_bottom-H4);
        metroPath3.lineTo(x-17,metro_bottom-H4);
        metroPath3.close();
        canvas.drawPath(metroPath3,metroPaint3);

        metroPath3.reset();
        metroPath3.moveTo(x+17,metro_bottom-H3);
        metroPath3.lineTo(x+2,metro_bottom-H3);
        metroPath3.lineTo(x+2,metro_bottom-H4);
        metroPath3.lineTo(x+17,metro_bottom-H4);
        metroPath3.close();
        canvas.drawPath(metroPath3,metroPaint3);

        Paint metroPaintmargin =new Paint();
        metroPaintmargin.setColor(getResources().getColor(R.color.metro_margin));
        metroPaintmargin.setAntiAlias(true);
        metroPaintmargin.setStyle(Paint.Style.FILL);
        Path pmargin=new Path();
        pmargin.moveTo(x-line3,metro_bottom-H3);
        pmargin.lineTo(x-line3+15,metro_bottom-H3);
        pmargin.lineTo(x-line4+15,metro_bottom-H4);
        pmargin.lineTo(x-line4,metro_bottom-H4);
        canvas.drawPath(pmargin,metroPaintmargin);
        pmargin.reset();
        pmargin.moveTo(x+line3,metro_bottom-H3);
        pmargin.lineTo(x+line3-15,metro_bottom-H3);
        pmargin.lineTo(x+line4-15,metro_bottom-H4);
        pmargin.lineTo(x+line4,metro_bottom-H4);
        canvas.drawPath(pmargin,metroPaintmargin);

        Paint metroPaintShade =new Paint();
        metroPaintShade.setColor(getResources().getColor(R.color.metro_shade));
        metroPaintShade.setAntiAlias(true);
        metroPaintShade.setStyle(Paint.Style.FILL);
        float lshade=100;
        float Hshade=(1-lshade/line2)*triangleH;
        Path pshade=new Path();
        pshade.moveTo(x+line3,metro_bottom-H3);
        pshade.lineTo(x-line3,metro_bottom-H3);
        pshade.lineTo(x-lshade,metro_bottom-Hshade);
        canvas.drawPath(pshade,metroPaintShade);

    }


}
